/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.sound;

import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

/**
 * @author Fred
 */
public class Sound {

	URL audioFile;
	Clip clip;

	public Sound(final URL audioFile) {
		this.audioFile = audioFile;
	}

	public void initialize() throws Exception {
		try {
			final AudioInputStream input = AudioSystem.getAudioInputStream(this.audioFile);
			input.getFormat();

			this.clip = AudioSystem.getClip();
			this.clip.open(input);
		} catch (final IllegalArgumentException iae) {
			this.clip = null;
		}

	}

	public void play() {
		try {
			if (this.clip != null) {
				this.clip.loop(1);
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

}
